//
//  DCFNormalView.m
//  DCF
//
//  Created by 吴文拼 on 2017/12/26.
//  Copyright © 2017年 吴文拼. All rights reserved.
//

#import "DCFNormalView.h"

@implementation DCFNormalView

-(void)set_dcf_EnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left{
    self.topClickOffset = top;
    self.rightClickOffset = right;
    self.bottomClickOffset = bottom;
    self.leftClickOffset = left;
}

- (CGRect) enlargedRect
{
    return CGRectMake(self.bounds.origin.x - self.leftClickOffset,
                      self.bounds.origin.y - self.topClickOffset,
                      self.bounds.size.width + self.leftClickOffset + self.rightClickOffset,
                      self.bounds.size.height + self.topClickOffset + self.bottomClickOffset);
}

- (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
    CGRect rect = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds))
    {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) && !self.hidden ? self : nil;
}

- (void) set_dcf_backgroundColor:(UIColor *)backgroundColor{
    self.backgroundColor = backgroundColor;
}

- (void) set_dcf_layerCornerRadius:(CGFloat)cornerRadius{
    self.layer.cornerRadius = cornerRadius;
}

- (void) set_dcf_layerBackgroundColor:(UIColor *)backgroundColor{
    self.layer.backgroundColor = backgroundColor.CGColor;
}

@end

@implementation DCFNormalLabel

-(void)set_dcf_EnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left{
    self.topClickOffset = top;
    self.rightClickOffset = right;
    self.bottomClickOffset = bottom;
    self.leftClickOffset = left;
}

- (CGRect) enlargedRect
{
    return CGRectMake(self.bounds.origin.x - self.leftClickOffset,
                      self.bounds.origin.y - self.topClickOffset,
                      self.bounds.size.width + self.leftClickOffset + self.rightClickOffset,
                      self.bounds.size.height + self.topClickOffset + self.bottomClickOffset);
}

- (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
    CGRect rect = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds))
    {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) && !self.hidden ? self : nil;
}


- (void) set_dcf_textAligment:(NSTextAlignment)textAligment{
    self.textAlignment = textAligment;
}

- (void)set_dcf_font:(UIFont *)font{
    self.font = font;
}

- (void)set_dcf_textColor:(UIColor *)textColor{
    self.textColor = textColor;
}

- (void) set_dcf_backgroundColor:(UIColor *)backgroundColor{
    self.backgroundColor = backgroundColor;
}

- (void) set_dcf_layerCornerRadius:(CGFloat)cornerRadius{
    self.layer.cornerRadius = cornerRadius;
}

- (void) set_dcf_layerBackgroundColor:(UIColor *)backgroundColor{
    self.layer.backgroundColor = backgroundColor.CGColor;
}


@end


@implementation DCFNormalImageView

-(void)set_dcf_EnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left{
    self.topClickOffset = top;
    self.rightClickOffset = right;
    self.bottomClickOffset = bottom;
    self.leftClickOffset = left;
}

- (CGRect) enlargedRect
{
    return CGRectMake(self.bounds.origin.x - self.leftClickOffset,
                      self.bounds.origin.y - self.topClickOffset,
                      self.bounds.size.width + self.leftClickOffset + self.rightClickOffset,
                      self.bounds.size.height + self.topClickOffset + self.bottomClickOffset);
}

- (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
    CGRect rect = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds))
    {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) && !self.hidden ? self : nil;
}

- (void) set_dcf_backgroundColor:(UIColor *)backgroundColor{
    self.backgroundColor = backgroundColor;
}

- (void) set_dcf_image:(UIImage *)image{
    self.image = image;
}

- (void) set_dcf_layerCornerRadius:(CGFloat)cornerRadius{
    self.layer.cornerRadius = cornerRadius;
}

- (void) set_dcf_layerBackgroundColor:(UIColor *)backgroundColor{
    self.layer.backgroundColor = backgroundColor.CGColor;
}

@end


@implementation DCFButton

-(void)set_dcf_EnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left{
    self.topClickOffset = top;
    self.rightClickOffset = right;
    self.bottomClickOffset = bottom;
    self.leftClickOffset = left;
}

- (CGRect) enlargedRect
{
    return CGRectMake(self.bounds.origin.x - self.leftClickOffset,
                      self.bounds.origin.y - self.topClickOffset,
                      self.bounds.size.width + self.leftClickOffset + self.rightClickOffset,
                      self.bounds.size.height + self.topClickOffset + self.bottomClickOffset);
}

- (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
    CGRect rect = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds))
    {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) && !self.hidden ? self : nil;
}


- (void)set_dcf_Title:(nullable NSString *)title forState:(UIControlState)state{
    [self setTitle:title forState:state];
}
- (void)set_dcf_TitleColor:(nullable UIColor *)color forState:(UIControlState)state{
    [self setTitleColor:color forState:state];
}
- (void)set_dcf_TitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state{
    [self setTitleShadowColor:color forState:state];
}
- (void)set_dcf_Image:(nullable UIImage *)image forState:(UIControlState)state{
    [self setImage:image forState:state];
}
- (void)set_dcf_BackgroundImage:(nullable UIImage *)image forState:(UIControlState)state{
    [self setBackgroundImage:image forState:state];
}
- (void)set_dcf_AttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state{
    [self setAttributedTitle:title forState:state];
}
- (void)set_dcf_layerCornerRadius:(CGFloat)cornerRadius{
    self.layer.cornerRadius = cornerRadius;
}
- (void)set_dcf_BackgroundColor:(UIColor *)color forState:(UIControlState)state{
    [self set_dcf_BackgroundImage:[self imageWithColor:color] forState:state];
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end

@implementation DCFPageControl

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self set_dcf_currentPage:0];
}

- (void)set_dcf_currentPage:(NSInteger)currentPage
{
    [super setCurrentPage:currentPage];
    
    for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++)
    {
        UIView * subview = [self.subviews objectAtIndex:subviewIndex];
        
        CGSize size;
        
        if (subviewIndex == currentPage)
        {
            size = CGSizeMake(8, 8);
            [subview setFrame:CGRectMake(subview.frame.origin.x-2, subview.frame.origin.y-2,size.width,size.height)];
        }
        else
        {
            size = CGSizeMake(6, 6);
            [subview setFrame:CGRectMake( subview.frame.origin.x, subview.frame.origin.y,size.width,size.height)];
        }
    }
}

@end


@implementation DCFTextField

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}  


@end

